﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.TownshipEnterprise.BLL;
using Yipusoft.TownshipEnterprise.Model;
using System.IO;
using System.Text;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;

public partial class TownshipEnterprise_DeviceStat : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            init();
        }
    }

    void init()
    {

        DeviceClassList.DataTextField = "ItemName";
        DeviceClassList.DataValueField = "ItemID";
        DeviceClassList.DataSource = CodeItem.GetList(" parentID='ZBQC010000' order by ItemID,SeqID ");
        DeviceClassList.DataBind();
    }

    HtmlTableCell td;
    HtmlTableRow tr;
    void Bind()
    {
        if (DeviceClassList.SelectedValue != "")
        {
            //所有的部门
            List<DepartmentInfo> departments = Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");
            //所有的大类型
            List<CodeItemInfo> XFC00 = CodeItem.GetList(" parentID='" + DeviceClassList.SelectedValue + "' order by ItemID,SeqID ");

            //头部分
            tr = new HtmlTableRow();
            tr.Cells.Add(new HtmlTableCell());

            tr.Cells[0].InnerText = "装备器材类型";
            tr.Cells[0].Attributes.Add("rowspan", "2");
            tr.Cells[0].Attributes.Add("align", "center");
            tr.Cells[0].Attributes.Add("style", "border-color:#CEE7F7;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

            for (int i = 0; i < departments.Count; i++)
            {
                td = new HtmlTableCell();
                td.InnerText = departments[i].AliasName;
                td.Attributes.Add("colspan", "2");
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
            }
            table1.Rows.Add(tr);


            tr = new HtmlTableRow();
            for (int i = 0; i < departments.Count; i++)
            {
                //td = new HtmlTableCell();
                //td.InnerText = "应配";

                //td.Attributes.Add("align", "center");
                //td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                //tr.Cells.Add(td);
                td = new HtmlTableCell();
                td.InnerText = "实配";

                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
                td = new HtmlTableCell();
                td.InnerText = "备份";

                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap; border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
            }
            table1.Rows.Add(tr);
            //中部
            for (int k = 0; k < XFC00.Count; k++)
            {
                tr = new HtmlTableRow();
                td = new HtmlTableCell();
                td.InnerText = XFC00[k].ItemName;
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap; border-width: 1px; border-style:solid;");

                tr.Cells.Add(td);
                for (int i = 0; i < departments.Count; i++)
                {
                    string departmentid = departments[i].DepartmentID;
                    //应配
                    //td = new HtmlTableCell();
                    //string VehicleClass = XFC00[k].ItemID;
                    //td.InnerText = GetItemCount(VehicleClass, departmentid);
                    //td.Attributes.Add("align", "center");
                    //td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");
                    //tr.Cells.Add(td);
                    //实配
                    td = new HtmlTableCell();

                    string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass='" + XFC00[k].ItemID + "' and State='ZBQC030001' ";
                    int count = TownDevices.GetCount(Where);
                    HtmlAnchor anchor = new HtmlAnchor();
                    anchor.InnerText = count.ToString();
                    anchor.HRef = "DeviceList.aspx?Where=" + Where + " ";
                    td.Controls.Add(anchor);
                    td.Attributes.Add("align", "center");
                    td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                    tr.Cells.Add(td);
                    //备份
                    td = new HtmlTableCell();
                    Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass='" + XFC00[k].ItemID + "' and State='ZBQC030002' ";
                    count = TownDevices.GetCount(Where);
                    anchor = new HtmlAnchor();
                    anchor.InnerText = count.ToString();
                    anchor.HRef = "DeviceList.aspx?Where=" + Where + " ";
                    td.Controls.Add(anchor);
                    td.Attributes.Add("align", "center");
                    td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

                    tr.Cells.Add(td);
                }
                table1.Rows.Add(tr);
            }
            //尾部
            tr = new HtmlTableRow();
            td = new HtmlTableCell();
            td.InnerText = "总计";
            td.Attributes.Add("align", "center");
            td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap; border-width: 1px; border-style:solid;");

            tr.Cells.Add(td);
            for (int i = 0; i < departments.Count; i++)
            {
                string departmentid = departments[i].DepartmentID;
                //应配
                //td = new HtmlTableCell();

                string VehicleClass = DeviceClassList.SelectedValue;
                //td.InnerText = GetItemSum(VehicleClass, departmentid);
                //td.Attributes.Add("align", "center");
                //td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");

                //tr.Cells.Add(td);
                //实配
                td = new HtmlTableCell();


                string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass like '" + VehicleClass.Substring(0, 8) + "%' and State='ZBQC030001' ";
                int count = TownDevices.GetCount(Where);

                HtmlAnchor anchor = new HtmlAnchor();

                anchor.InnerText = count.ToString();
                anchor.HRef = "DeviceList.aspx?Where=" + Where + " ";
                td.Controls.Add(anchor);
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

                tr.Cells.Add(td);
                //备份
                td = new HtmlTableCell();
                Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass like '" + VehicleClass.Substring(0, 8) + "%' and State='ZBQC030002' ";
                count = TownDevices.GetCount(Where);
                anchor = new HtmlAnchor();
                // anchor.Title = count.ToString();
                anchor.InnerText = count.ToString();
                anchor.HRef = "DeviceList.aspx?Where=" + Where + " ";
                td.Controls.Add(anchor);
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#CEE7F7;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

                tr.Cells.Add(td);
            }
            table1.Rows.Add(tr);
            table1.Attributes.Add("style", "border-color: #CEE7F7;border-collapse: collapse;  border-width: 1px; border-style:solid;");

        }

    }
    protected void DeviceClassList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind();
    }

    public string GetItemCount(string itemID, string DepartmentID)
    {
        if (EquipStandard.Exsits(itemID))
        {
            List<Yipusoft.Listener.Model.Departments> depart = Yipusoft.Listener.BLL.Department.GetList("DepartmentID like '" + DepartmentID.Substring(0, 4) + "%' and   SUBSTRING(DepartmentID, 5, 1)<>0 ");

            EquipStandardInfo info = EquipStandard.GetEquipStandard(itemID);
            int count = 0;
            if (info.ComputeBy == "人")
            {
                int FighterNumbers = depart.Sum(o => o.FighterNumber.Value);
                count = FighterNumbers * info.ItemCount;
            }
            else if (info.ComputeBy == "班")
            {
                int GroupNumber = depart.Sum(o => o.GroupNumber.Value);
                count = GroupNumber * info.ItemCount;
            }
            else if (info.ComputeBy == "站")
            {
                int DepartCount = depart.Count;
                count = DepartCount * info.ItemCount;
            }
            return count.ToString();
        }
        else
            return "0";
    }

    public string GetItemSum(string itemID, string DepartmentID)
    {
        List<Yipusoft.Listener.Model.Departments> depart = Yipusoft.Listener.BLL.Department.GetList("DepartmentID like '" + DepartmentID.Substring(0, 4) + "%' and   SUBSTRING(DepartmentID, 5, 1)<>0 ");
        //所有的装备
        List<CodeItemInfo> XFC00 = CodeItem.GetList(" parentID='" + DeviceClassList.SelectedValue + "'  or ItemID='" + DeviceClassList.SelectedValue + "' order by SeqID ");
        var p = (from xfc in XFC00 select xfc.ItemID).ToList();
        List<EquipStandardInfo> list = EquipStandard.GetList("").Where(o => p.Contains(o.ItemID)).ToList();

        int count = 0;
        foreach (var info in list)
        {
            if (info.ComputeBy == "人")
            {
                int FighterNumbers = depart.Sum(o => o.FighterNumber.Value);
                count += FighterNumbers * info.ItemCount;
            }
            else if (info.ComputeBy == "班")
            {
                int GroupNumber = depart.Sum(o => o.GroupNumber.Value);
                count += GroupNumber * info.ItemCount;
            }
            else if (info.ComputeBy == "站")
            {
                int DepartCount = depart.Count;
                count += DepartCount * info.ItemCount;
            }
        }
        return count.ToString();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ExportBind();
        ExportData("application/ms-excel", DateTime.Now.ToString("yyMdHm") + ".xls");
    }

    private void ExportData(string FileType, string FileName)
    {
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        StringWriter tw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(tw);
        table1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }

    void ExportBind()
    {
        if (DeviceClassList.SelectedValue != "")
        {
            //所有的部门
            List<DepartmentInfo> departments = Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");
            //所有的大类型
            List<CodeItemInfo> XFC00 = CodeItem.GetList(" parentID='" + DeviceClassList.SelectedValue + "' order by ItemID,SeqID ");

            //头部分
            tr = new HtmlTableRow();
            tr.Cells.Add(new HtmlTableCell());

            tr.Cells[0].InnerText = "装备器材类型";
            tr.Cells[0].Attributes.Add("rowspan", "2");
            tr.Cells[0].Attributes.Add("align", "center");
            tr.Cells[0].Attributes.Add("style", "border-color:#000;border-collapse:  collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

            for (int i = 0; i < departments.Count; i++)
            {
                td = new HtmlTableCell();
                td.InnerText = departments[i].AliasName;
                td.Attributes.Add("colspan", "2");
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
            }
            table1.Rows.Add(tr);


            tr = new HtmlTableRow();
            for (int i = 0; i < departments.Count; i++)
            {
                td = new HtmlTableCell();
                //td.InnerText = "应配";

                //td.Attributes.Add("align", "center");
                //td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                //tr.Cells.Add(td);
                //td = new HtmlTableCell();
                td.InnerText = "实配";

                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
                td = new HtmlTableCell();
                td.InnerText = "备份";

                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap; border-width: 1px; border-style:solid;");
                tr.Cells.Add(td);
            }
            table1.Rows.Add(tr);
            //中部
            for (int k = 0; k < XFC00.Count; k++)
            {
                tr = new HtmlTableRow();
                td = new HtmlTableCell();
                td.InnerText = XFC00[k].ItemName;
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap; border-width: 1px; border-style:solid;");

                tr.Cells.Add(td);
                for (int i = 0; i < departments.Count; i++)
                {
                    string departmentid = departments[i].DepartmentID;
                    //应配
                    td = new HtmlTableCell();
                    string VehicleClass = XFC00[k].ItemID;
                    //td.InnerText = GetItemCount(VehicleClass, departmentid);
                    //td.Attributes.Add("align", "center");
                    //td.Attributes.Add("style", "border-color:#000;border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");
                    //tr.Cells.Add(td);
                    //实配
                    td = new HtmlTableCell();

                    string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass='" + XFC00[k].ItemID + "' and State='ZBQC030001' ";
                    int count = Device.GetCount(Where);
                    HtmlAnchor anchor = new HtmlAnchor();
                    anchor.InnerText = count.ToString();
            
                    td.Controls.Add(anchor);
                    td.Attributes.Add("align", "center");
                    td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");
                    tr.Cells.Add(td);
                    //备份
                    td = new HtmlTableCell();
                    Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass='" + XFC00[k].ItemID + "' and State='ZBQC030002' ";
                    count = Device.GetCount(Where);
                    anchor = new HtmlAnchor();
                    anchor.InnerText = count.ToString();
          
                    td.Controls.Add(anchor);
                    td.Attributes.Add("align", "center");
                    td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

                    tr.Cells.Add(td);
                }
                table1.Rows.Add(tr);
            }
            //尾部
            tr = new HtmlTableRow();
            td = new HtmlTableCell();
            td.InnerText = "总计";
            td.Attributes.Add("align", "center");
            td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap; border-width: 1px; border-style:solid;");

            tr.Cells.Add(td);
            for (int i = 0; i < departments.Count; i++)
            {
                string departmentid = departments[i].DepartmentID;
                //应配
                td = new HtmlTableCell();

                string VehicleClass = DeviceClassList.SelectedValue;
                //td.InnerText = GetItemSum(VehicleClass, departmentid);
                //td.Attributes.Add("align", "center");
                //td.Attributes.Add("style", "border-color:#000;border-collapse: collapse;white-space:nowrap;  border-width: 1px; border-style:solid;");

                //tr.Cells.Add(td);
                //实配
                td = new HtmlTableCell();


                string Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass like '" + VehicleClass.Substring(0, 8) + "%' and State='ZBQC030001' ";
                int count = Device.GetCount(Where);

                HtmlAnchor anchor = new HtmlAnchor();

                anchor.InnerText = count.ToString();
                td.Controls.Add(anchor);
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

                tr.Cells.Add(td);
                //备份
                td = new HtmlTableCell();
                Where = " DepartmentID like '" + departmentid.Substring(0, 4) + "%' and  DeviceClass like '" + VehicleClass.Substring(0, 8) + "%' and State='ZBQC030002' ";
                count = Device.GetCount(Where);
                anchor = new HtmlAnchor();
                // anchor.Title = count.ToString();
                anchor.InnerText = count.ToString();
                td.Controls.Add(anchor);
                td.Attributes.Add("align", "center");
                td.Attributes.Add("style", "border-color:#000;border-collapse: collapse; white-space:nowrap;  border-width: 1px; border-style:solid;");

                tr.Cells.Add(td);
            }
            table1.Rows.Add(tr);
            table1.Attributes.Add("style", "border-color: #000;border-collapse: collapse;  border-width: 1px; border-style:solid;");

        }

    }
}

